package com.miracle9.game.service;

import com.miracle9.game.dao.PayLogServiceDao;
import com.miracle9.game.dao.PromoterServiceDao1;
import com.miracle9.game.dao.UserAwardServiceDao;
import com.miracle9.game.dao.UserServiceDao;
import com.miracle9.game.dao.ComputerOrderServiceDao;
import com.miracle9.game.dao.DayAccountsServiceDao;
import com.miracle9.game.entity.DayAccounts;
import com.miracle9.game.util.MyUtil;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("dayAccountsService")
public class DayAccountsService {

    @Autowired
    private DayAccountsServiceDao dayAccountsServiceDao;
    @Autowired
    private UserServiceDao userServiceDao;
    @Autowired
    private PayLogServiceDao payLogServiceDao;
    @Autowired
    private UserAwardServiceDao userAwardServiceDao;
    @Autowired
    private ComputerOrderServiceDao ComputerOrderServiceDao;
    @Autowired
    private PromoterServiceDao1 promoterServiceDao1;
    @Autowired
    private UserService userService;

    public void createAccounts(String date) throws Exception {
        DayAccounts dayAccounts = generateAccounts(date);
        this.dayAccountsServiceDao.save(dayAccounts);
    }

    public DayAccounts generateAccounts(String date) throws Exception {
        List<Object[]> sumPayLog = this.payLogServiceDao.getSumByDate(date);
        int sumAward = this.userAwardServiceDao.getSumGoldByDate(date);
        int sumDeposit = this.ComputerOrderServiceDao.getSumDepositByBorrowDate(date);
        int sumReturnDeposit = this.ComputerOrderServiceDao.getSumDepositByRepayDate(date);

        DayAccounts dayAccounts = new DayAccounts();
        for (Object[] o : sumPayLog) {
            if (Integer.valueOf(o[0].toString()).intValue() == 0) {
                double charge = Double.valueOf(o[1].toString()).doubleValue();
                dayAccounts.setPayCount((int) charge);
            } else if (1 == Integer.valueOf(o[0].toString()).intValue()) {
                double change = Double.valueOf(o[1].toString()).doubleValue();
                dayAccounts.setExchangeCount(change);
            } else if (2 == Integer.valueOf(o[0].toString()).intValue()) {
                int minus = Integer.valueOf(o[2].toString()).intValue();
                dayAccounts.setDeductGold(minus);
            }
        }
        dayAccounts.setGiveGold(sumAward);
        dayAccounts.setTakeDeposit(sumDeposit);
        dayAccounts.setReturnDeposit(sumReturnDeposit);
        dayAccounts.setDatetime(date);
        dayAccounts.setSumGold(this.userServiceDao.getSumGameGoldByPromoterId_0() + this.promoterServiceDao1.getSumGoldByparentId_0());
        double revenue = dayAccounts.getPayCount() - dayAccounts.getExchangeCount() + dayAccounts.getTakeDeposit() - dayAccounts.getReturnDeposit();
        revenue = MyUtil.returnBigDecimalDoubleValue(revenue);
        dayAccounts.setRevenue(revenue);
        long nGold = this.userService.getClearingScore();
        dayAccounts.setAllSumGold(this.userServiceDao.getSumGameGold() + this.promoterServiceDao1.getSumGold() + nGold);
        return dayAccounts;
    }

}
